﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;

namespace Ocean.His.MedicalInsurance
{
    public class MipDictionaryDataSeederContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IRepository<MedicalInsuranceDictionary, Guid> _dataDictionaryRepository;
        private readonly IRepository<MedicalInsuranceDictionaryType, Guid> _dataDictionaryTypeRepository;

        public MipDictionaryDataSeederContributor(IRepository<MedicalInsuranceDictionary, Guid> dataDictionaryRepository
            , IRepository<MedicalInsuranceDictionaryType, Guid> dataDictionaryTypeRepository)
        {
            _dataDictionaryRepository = dataDictionaryRepository;
            _dataDictionaryTypeRepository = dataDictionaryTypeRepository;
        }

        #region 医疗类别

        public const string MED_TYPE_VALUE = "med_type";
        public const string MED_TYPE_LABEL = "医疗类别";

        public const string DCLA_SOUC_VALUE = "dcla_souc";
        public const string DCLA_SOUC_LABEL = "申报来源";

        #endregion

        public async Task SeedAsync(DataSeedContext context)
        {
            #region 医疗类别

            await InsertDataDictionary(MED_TYPE_VALUE, MED_TYPE_LABEL);
            await InsertDataDictionary(DCLA_SOUC_VALUE, DCLA_SOUC_LABEL);

            #endregion
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="label"></param>
        /// <returns></returns>
        private async Task InsertDataDictionary(string value, string label)
        {
            if (await _dataDictionaryTypeRepository.AnyAsync(d => d.Value == value))
            {
                var delIds = (await _dataDictionaryTypeRepository.GetListAsync()).Select(d => d.Id).ToList();

                foreach (var delId in delIds)
                {
                    await _dataDictionaryTypeRepository.DeleteAsync(delId);
                }
            }

            await _dataDictionaryTypeRepository.InsertAsync(new MedicalInsuranceDictionaryType { Value = value, Label = label, IsValid = true });

        }

        private async Task InsertDataDictionary(int index , string type, string value, string label)
        {
            var iDataDictionaryQuery = await _dataDictionaryRepository.GetQueryableAsync();

            if (!iDataDictionaryQuery.Any(d => d.Type == type && d.Value == value))
            {
                MedicalInsuranceDictionary dataDictionary = new MedicalInsuranceDictionary();
                dataDictionary.Type = type;
                dataDictionary.Value = value;
                dataDictionary.Label = label;
                dataDictionary.Sort = index;
                dataDictionary.Version = "1";
                await _dataDictionaryRepository.InsertAsync(dataDictionary);
            }
        }
    }
}
